//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartVirtualSeriesView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartVirtualSeriesView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CBCGPChartValue CALLBACK ChartCallbackFunc(double dblXValue, CBCGPChartSeries* /*pFormulaSeries*/, LPARAM lp)
{
	int nFormula = (int)lp;

	CBCGPChartValue valResult;

	switch (nFormula)
	{
	case 0:
		valResult = dblXValue * dblXValue;
		break;

	case 1:
		if (dblXValue > 0)
		{
			valResult = log(dblXValue);
		}
		break;

	case 2:
		valResult = exp(-dblXValue / 10) * sin(dblXValue);
		break;

	case 3:
		if (dblXValue >= 0)
		{
			valResult = sqrt(dblXValue);
		}
		break;
	}

	return valResult;
}

/////////////////////////////////////////////////////////////////////////////
// CChartVirtualSeriesView

IMPLEMENT_DYNCREATE(CChartVirtualSeriesView, CBCGPChartExampleView)

CChartVirtualSeriesView::CChartVirtualSeriesView()
	: CBCGPChartExampleView(CChartVirtualSeriesView::IDD)
{
	//{{AFX_DATA_INIT(CChartVirtualSeriesView)
	m_nCallbackType = 0;
	m_nXRange = 1;
	m_strXRange = _T("");
	//}}AFX_DATA_INIT

	m_bDisableAnimation = FALSE;
}

CChartVirtualSeriesView::~CChartVirtualSeriesView()
{
}

void CChartVirtualSeriesView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartVirtualSeriesView)
	DDX_Control(pDX, IDC_X_RANGE, m_wndXRange);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_CALLBACK_TYPE, m_nCallbackType);
	DDX_Slider(pDX, IDC_X_RANGE, m_nXRange);
	DDX_Text(pDX, IDC_X_RANGE_VALUE, m_strXRange);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartVirtualSeriesView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartVirtualSeriesView)
	ON_CBN_SELENDOK(IDC_CALLBACK_TYPE, OnUpdateChart)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartVirtualSeriesView diagnostics

#ifdef _DEBUG
void CChartVirtualSeriesView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartVirtualSeriesView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartVirtualSeriesView message handlers

void CChartVirtualSeriesView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->CreateSeries(_T("Virtual Series"));

	m_wndXRange.SetRange(1, 10, TRUE);

	pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS)->SetDisplayDataBetweenTickMarks(FALSE);

	SetDefaultLineWidth();
	OnUpdateChart();
}

void CChartVirtualSeriesView::OnUpdateChart() 
{
	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartSeries* pSeries = pChart->GetSeries(0);
	ASSERT_VALID(pSeries);

	pSeries->SetVirtualMode(TRUE, ChartCallbackFunc, m_nCallbackType, 0, m_nXRange * 10, m_bDisableAnimation);
	m_strXRange.Format(_T("[0 - %d]"), m_nXRange * 10);

	UpdateData(FALSE);

	if (!m_bDisableAnimation)
	{
		OnChartAnimation();
	}

	m_bDisableAnimation = FALSE;
}

void CChartVirtualSeriesView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if (pScrollBar->GetSafeHwnd() != m_wndXRange.GetSafeHwnd())
	{
		CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);
		return;
	}

	m_bDisableAnimation = TRUE;

	OnUpdateChart();
}
